package com.sports.demo.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sports.demo.basic.BasicController;
import com.sports.demo.dao.AppointmentRecordMapper;
import com.sports.demo.dao.VenueMapper;
import com.sports.demo.model.AppointmentRecord;
import com.sports.demo.model.Facilities;
import com.sports.demo.req.AppointmentRecordResp;
import com.sports.demo.req.NameReq;
import com.sports.demo.service.AppointmentRecordService;
import com.sports.demo.service.FacilitiesService;
import com.sports.demo.service.UserService;
import com.sports.demo.service.VenueService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 预约记录 前端控制器
 * </p>
 *
 * @author SRG
 * @since 2022-03-13
 */
@RestController
@RequestMapping("/appointmentRecord")
public class AppointmentRecordController extends BasicController<AppointmentRecordService, AppointmentRecord> {
  @Resource
  private VenueService venueService;
  @Resource
  private UserService userService;
  @Resource
  private FacilitiesService facilitiesService;


  @PostMapping({"getPage"})
  public IPage<AppointmentRecordResp> page(@RequestBody NameReq req) {
    LambdaQueryWrapper<AppointmentRecord> query = Wrappers.lambdaQuery(AppointmentRecord.class).orderByDesc(AppointmentRecord::getId);
    IPage<AppointmentRecord> page = getBasicService().page(req.getPlusPage(), query);

    IPage<AppointmentRecordResp> pages = new Page<>(page.getCurrent(),page.getSize(),page.getTotal());
    List<AppointmentRecord> list = page.getRecords();
    List<AppointmentRecordResp> list1 = new ArrayList<>();
    for (AppointmentRecord appointmentRecord : list) {
      AppointmentRecordResp recordResp = new AppointmentRecordResp();
      BeanUtils.copyProperties(appointmentRecord, recordResp);
      recordResp.setVenueName(venueService.getById(appointmentRecord.getVenueId()).getVenueName());
      recordResp.setFacilitiesIName(facilitiesService.getById(appointmentRecord.getFacilitiesId()).getFacilitiesName());
      recordResp.setType(facilitiesService.getById(appointmentRecord.getFacilitiesId()).getType());
      recordResp.setUserName(userService.getById(appointmentRecord.getUserId()).getName());
      list1.add(recordResp);
    }
    pages.setRecords(list1);
    return pages;
  }

  @PostMapping("getList")
  @ApiOperation(value = "列表", notes = "")
  public List<AppointmentRecordResp> getList(@RequestBody AppointmentRecord search) {

    List<AppointmentRecord> list = getBasicService().list(Wrappers.lambdaQuery(AppointmentRecord.class)
        .orderByAsc(AppointmentRecord::getId)
        .setEntity(search));
    List<AppointmentRecordResp> list1 = new ArrayList<>();

    for (AppointmentRecord appointmentRecord : list) {
      AppointmentRecordResp recordResp = new AppointmentRecordResp();
      BeanUtils.copyProperties(appointmentRecord, recordResp);
      recordResp.setVenueName(venueService.getById(appointmentRecord.getVenueId()).getVenueName());
      recordResp.setType(facilitiesService.getById(appointmentRecord.getFacilitiesId()).getType());
      recordResp.setFacilitiesIName(facilitiesService.getById(appointmentRecord.getFacilitiesId()).getFacilitiesName());
      recordResp.setUserName(userService.getById(appointmentRecord.getUserId()).getName());
      list1.add(recordResp);

    }

    return list1;
  }


  @PostMapping("use")
  @ApiOperation(value = "变更为已使用", notes = "")
  public boolean use(@RequestBody AppointmentRecord req) {
    AppointmentRecord appointmentRecord = getBasicService().getById(req.getId());
    appointmentRecord.setStat(req.getStat());
    return getBasicService().updateForUniqueKey(appointmentRecord);
  }
}
